home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.9 KB  |  95 lines

  1. // CmRing.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Ring (circular linked list) definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMRING_H
  10. #define _CMRING_H
  11.  
  12. #include <cm/include/cmcont.h>
  13.  
  14. class CmRingIterator;                        // Ring iterator class stub.
  15. class CmRingNode;                            // Ring node class stub.
  16.  
  17. class CmRing : public CmContainer {          // Ring definition.
  18. public:
  19.   CmRing() : _top(NULL), _last(NULL) {}      // Default ring constructor.
  20.   CmRing(const CmRing&);                     // Ring copy constructor.
  21.  ~CmRing();                                  // Ring destructor.
  22.  
  23.   CmRing& operator=(const CmRing&);          // Assignment operator.
  24.  
  25.   Bool        add        (CmObject*);        // Add object to ring top.
  26.   Bool        remove     (CmObject*);        // Remove equal object.
  27.   Bool        removeTop  ();                 // Remove top object.
  28.   CmObject*   lookup     (CmObject*) const;  // Return equal object.
  29.   Bool        contains   (CmObject*) const;  // See if ring contains equal.
  30.   unsigned    occurrences(CmObject*) const;  // Count number of equal objects.
  31.   void        removeAll  ();                 // Remove all objects from ring.
  32.   CmObject*   top        () const;           // Get pointer to top object.
  33.   CmIterator* newIterator() const;           // Create and return iterator.
  34.  
  35.   CMOBJECT_DEFINE(CmRing, CmContainer)       // Define object funcs.
  36.  
  37. protected:
  38.   CmRingNode *_top;                          // Top node in ring.
  39.   CmRingNode *_last;                         // Last node in ring.
  40.   friend      CmRingIterator;                // Iterator can access.
  41. };
  42.  
  43. class CmRingIterator : public CmIterator {   // Iterator definition.
  44. public:
  45.   CmRingIterator(const CmRing& R)            // Iterator constructor.
  46.                 : _ring(R), _node(R._top) {}
  47.  
  48.   Bool      done    () const;                // See if done iterating (FALSE).
  49.   CmObject* next    ();                      // Get next object in ring.
  50.   CmObject* previous();                      // Return and backup.
  51.   CmObject* current () const;                // Get current ring object.
  52.   void      first   ();                      // Move to first object.
  53.   void      last    ();                      // Move to last object.
  54.  
  55.   CMOBJECT_DEFINE(CmRingIterator, CmIterator)  // Define object funcs.
  56.  
  57. protected:
  58.   CmRingNode   *_node;                       // Current ring node.
  59.   const CmRing &_ring;                       // Ring being iterated.
  60.   friend         CmRing;                     // Ring class can access.
  61. };
  62.  
  63. class CmRingNode {                           // Ring node definition.
  64. protected:
  65.   CmRingNode(CmObject *O)                    // Ring node constructor.
  66.             : _next(NULL), _data(O) {}
  67.  
  68.   CmRingNode *_next;                         // Next node in ring.
  69.   CmObject   *_data;                         // Pointer to object.
  70.   friend      CmRing;                        // Ring class can access.
  71.   friend      CmRingIterator;                // Ring iterator can access.
  72. };
  73.  
  74. // "top" returns a pointer to the top object in the ring.
  75. inline CmObject* CmRing::top() const
  76. { return (_top) ? _top->_data : NULL; }
  77.  
  78. // "done" checks to see if done iterating.  (Always FALSE for a ring.)
  79. inline Bool CmRingIterator::done() const
  80. { return FALSE; }
  81.  
  82. // "current" returns the current object in the ring.
  83. inline CmObject* CmRingIterator::current() const
  84. { return (_node) ? _node->_data : NULL; }
  85.  
  86. // "first" resets the iterator to the ring top.
  87. inline void CmRingIterator::first()
  88. { _node = _ring._top; }
  89.  
  90. // "last" moves the iterator to the last object.
  91. inline void CmRingIterator::last()
  92. { _node = _ring._last; }
  93.  
  94. #endif
  95.